# @Time:   2021/2/26 9:30
# @Author: Zong Wei

from time import sleep

from faker import Faker
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait

from ui_automation_testing.test.pageObject.loginPage import LoginPage
from ui_automation_testing.utils.logger import Logger


class ClientCreditPage(LoginPage):
    logger = Logger(__name__).get_log()
    menu = (By.XPATH, "//ul[@class='site-menu']//span[.='业务通用']")
    sub_menu = (By.XPATH, "//ul[@class='site-menu']//span[.='客户信用']")
    search_button = (By.ID, "lr-search")
    add_button = (By.ID, "lr-add")
    edit_button = (By.ID, "lr-edit")
    del_button = (By.ID, "lr-delete")
    save_button = (By.ID, "lr-save")
    clear_button = (By.ID, "lr-clear")
    close_button = (By.ID, "btn_close")

    client_name = (By.ID, "_easyui_textbox_input3")
    client_name_value = (By.ID, "datagrid-row-r3-2-0")
    agreement = (By.XPATH, "//select[@id='ParentAgrID']/following::a")
    agreement_value = (By.XPATH, "//*[@id='datagrid-row-r8-2-0']/td[@field='AgreementNo']/div")

    biz_unit = (By.XPATH, "//select[@id='ProductCategoryID']/following::a")
    biz_unit_value = (By.XPATH, "//*[@id='datagrid-row-r6-2-0']/td[@field='ProductCategoryName']/div")
    credit_limit = (By.ID, "AvgCredit")
    credit_rank = (By.ID, "CreditRating")
    credit_rank_value = (By.XPATH, "//div[@id='CreditRating-option']//li[2]")

    pay_type = (By.ID, "PayType")
    pay_type_value = (By.XPATH, "//div[@id='PayType-option']//li[2]")
    save_credit_button = (By.XPATH, "//span[.='保存']")
    partner_name = (By.ID, "PartnerNameCN")

    main_agreement = (By.ID, "ParentAgrID")

    credit_code = (By.ID, "creditCode")
    credit_name = (By.ID, "creditName")
    address = (By.ID, "Address")

    city_value = (By.ID, "datagrid-row-r3-2-0")
    province_id = (By.ID, "ProvinceID")
    city_id = (By.ID, "CityID")
    remark = (By.ID, "Remark")
    confirm_button = (By.XPATH, "//a[.='确认']")
    first_row = (By.XPATH, "//span[.='客户名称']//following::tr[1]")

    def __init__(self):
        self.driver = LoginPage().login()
        self.wait = WebDriverWait(self.driver, 10)

    def client_credit_home(self):
        self.logger.info("登录系统成功")
        self.wait.until(ec.element_to_be_clickable(self.menu)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.sub_menu)).click()
        sleep(1)
        return self.driver

    def add_credit(self, client_name='瓦克化学（中国）有限公司'):
        self.driver.switch_to.frame(1)
        self.wait.until(ec.element_to_be_clickable(self.add_button)).click()
        self.logger.info("添加客户信用")
        sleep(2)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame(2)
        self.wait.until(ec.presence_of_element_located(self.client_name)).send_keys(client_name)
        sleep(2)
        self.wait.until(ec.element_to_be_clickable(self.client_name_value)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.agreement)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.agreement_value)).click()
        sleep(1)
        global agreement_num
        agreement_num = self.driver.find_element_by_xpath(
            "//select[@id='ParentAgrID']/following::input[1]").get_property('value')
        print('主协议号：' + agreement_num)
        self.wait.until(ec.element_to_be_clickable(self.biz_unit)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.biz_unit_value)).click()
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.credit_limit)).send_keys('5000000')
        self.wait.until(ec.element_to_be_clickable(self.credit_rank)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.credit_rank_value)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.save_credit_button)).click()
        sleep(2)
        self.wait.until(ec.element_to_be_clickable(self.close_button)).click()
        self.logger.info("新增完成")

    def search_credit(self, credit_name='瓦克化学（中国）有限公司'):
        self.driver.switch_to.frame(1)
        self.wait.until(ec.presence_of_element_located(self.partner_name)).send_keys(credit_name)
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.main_agreement)).send_keys(agreement_num)
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.search_button)).click()
        sleep(2)
        self.wait.until(ec.element_to_be_clickable(self.clear_button)).click()
        self.logger.info("查询完成")

    def edit_credit(self):
        self.search_credit()
        self.wait.until(ec.element_to_be_clickable(self.first_row)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.edit_button)).click()
        sleep(1)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame(2)
        self.wait.until(ec.presence_of_element_located(self.remark)).send_keys(Faker().address())
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.save_credit_button)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.close_button)).click()
        self.logger.info("编辑修改完成")

    def del_credit(self):
        self.search_credit()
        self.wait.until(ec.element_to_be_clickable(self.first_row)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.del_button)).click()
        sleep(1)
        self.driver.switch_to.default_content()
        self.wait.until(ec.element_to_be_clickable(self.confirm_button)).click()
        self.logger.info("删除成功")


if __name__ == '__main__':
    pg = ClientCreditPage()
    pg.client_credit_home()
    pg.add_credit()

